<?php
class cmshopapiclient_Notification extends cmshopapiclient_base
{
    var $verb='Notification';
    var $fields=array(
        
    );
    function getRequest($requeststr){
        try{
            $DOM = new DOMDocument();
            if(@!$DOM->loadXML($requeststr)){
                throw new Exception_RequstXmlInvalidate('');
            }
            $Ack = $DOM -> getElementsByTagName('Ack') -> length > 0 ? $DOM -> getElementsByTagName("Ack") -> item(0) -> nodeValue : false;
            $NotificationEventName = $DOM -> getElementsByTagName("NotificationEventName") -> length > 0 ? $DOM -> getElementsByTagName("NotificationEventName") -> item(0) -> nodeValue : false;
            $Timestamp = $DOM -> getElementsByTagName('timestamp') -> length > 0 ? $DOM -> getElementsByTagName('Timestamp') -> item(0) -> nodeValue : false;
            if($Ack!='Success') throw new Exception_AckInvalidate(''); 
            switch($NotificationEventName){
                case 'OrderCreated':
                case 'OrderPayed':
                    $domTransaction=Helper_xml::domxml2a($DOM->getElementsByTagName('GetOrderTransaction'));
                    $this->receiveOrder($domTransaction);
                    break;
                default: break;
            }
        }catch(Exception_AckInvalidate $ex){
            echo 'Ack Fail !'; return false; 
        }catch(Exception_RequstXmlInvalidate $ex){
            echo 'Error Format,Not Xml Document!'; return false; //格式 错误 的请求 ,不记录
        }catch(Exception $ex){
            echo 'Any Error !'
            return false;
        }
    }
    
    // 回给 客户  .
    function Response($sessionid,$hardexpirationtime){
        $xmlArr=array(
            'Ack'=>'Success',
			'SessionID'=>$sessionid,
			'hardexpirationtime'=>$hardexpirationtime
		);
        return $this->xmlresponse($xmlArr);
    }
    
    // 接收到 订单 , 保存 , 更新 订单 . 
    function receiveOrder($order){
        $currency=$order['currency'];
        // 1 ,取得 接收订单的 用户 , 据 store_id 取 . 
        $order['store_id'] ;
		$uid=0;
        // 2, 保存客户
		$buyer=$order['buyer']['shipping_address'];
		$buyer['email'];
        $EC=Ebay_Customers::find('uid=? And email=?',uid,$buyer['email'])->getOne();
		if($EC->isNewRecord()){ //添加  新客户 
			$EC->userid=$buyer['email'];
            $EC->email=$buyer['email'];
			$EC->first_name=$buyer['first_name'];
			$EC->last_name=$buyer['last_name'];
			$EC->company=$buyer['company'];
			$EC->cityname=$buyer['cityname'];
			$EC->stateorprovince=$buyer['stateorprovince'];
			$EC->cityname=$buyer['cityname'];
			$EC->country=$buyer['country'];
			$EC->countryname=$buyer['countryname'];
			$EC->street1=$buyer['street1'];
			$EC->street2=$buyer['street2'];
			$EC->postalcode=$buyer['postalcode'];
			$EC->phone=$buyer['phone'];
			$EC->save();
		}
        //3 , 保存 订单
        $MO=new Ebay_Myorders::find('uid=? And order_from=? And ebay_orderid=?',$uid,'cmshop',$order['order_id'])->getOne();
		$oda=array(
            'finel_amount'=>$order['amount'],
            'shipping_cost'=>$order['shipping_cost'],
            'status_payment'=>$order['status_payment'],
            'status_shiped'=>$order['status_shiped'],
            'payment'=>$order['payment'],
            'paidtime'=>$order['paid_time'],
            
            'buyer_id'=>$EC->userid,
            'ecid'=>$EC->ecid,
            'ship_name'=>$buyer['first_name'].' '.$buyer['last_name'],
            'ship_stateorprovince'=>$buyer['stateorprovince'],
            'ship_phone'=>$buyer['phone'],
            'ship_street1'=>$buyer['street1'],
            'ship_street2'=>$buyer['street2'],
            'ship_email'=>$buyer['email'],
            'ship_cityname'=>$buyer['cityname'],
            'ship_country'=>$buyer['country'],
            'ship_postalcode'=>$buyer['postalcode'],
            'ship_countryname'=>$buyer['countryname'],
        );
        if($MO->isNewRecord()){
			$oda=$oda+ array(
				'uid'=>$uid,
				'order_from'=>'cmshop',
				'currency'=>$order['currency'],
				'created'=>$order['created'],
			);
		}
        $MO->changeProps($oda);
        $MO->save();
        $myorderid=$MO->myorderid;
        //4 , 保存 子订单  
        foreach($order['transactions'] as $dt){
            $t=Ebay_Transaction::find('uid=? And myorderid=? And transactionid=? ',$uid,$myorderid,$dt['transaction_id'])->getOne();
            // $goods_id
            // 
            $goods_id=0;
            $tda=array(
                'goods_id'=>$goods_id,
                'title'=>$dt['title'],
                'quantitypurchased'=>$dt['quantity_purchased'],
                'transactionprice'=>$dt['transaction_price'],
                'shippingservicecost'=>$dt['title'],
                'finalvaluefee'=>0,
//                'status_payment'=>$dt[''],
            );
            if($t->isNewRecord()){
                $tda=$tda+array(
                    'uid'=>$uid,
                    'myorderid'=>$myorderid,
                    'currency'=>$dt['currency'],
                    'platform'=>'cmshop',
                    'created'=>$dt['created'],
                    
                );
            }
            $t->changeProps($tda);
        }
    }
}
class Exception_AckInvalidate extends QException {}
class Exception_RequstXmlInvalidate extends QException {}
